package com.yxhl.zoume.core.func.map.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.yxhl.zoume.R;
import com.yxhl.zoume.UIConstants;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.func.map.ui.fragment.LocationMapFragment;
import com.yxhl.zoume.core.func.map.ui.fragment.POISearchFragment;
import com.yxhl.zoume.core.tripsmgmt.info.LocationMapEntrance;
import com.yxhl.zoume.utils.SoftInputUtils;

import butterknife.BindView;

public class LocationMapActivity extends BaseActivityWithToolBar {

    private static final String TAG = "SpecialStationMapAct";


    private static final String YI_YANG_CITY = "益阳市";
    public String mUserSelectCity = YI_YANG_CITY;
    @BindView(R.id.frame_location_map_body)
    FrameLayout mFrameLayout;
    //arguments
    private LocationMapEntrance mEntrance;
    private LocationMapFragment mLocationMapFragment;
    private POISearchFragment mPOISearchFragment;

    private Fragment mCurrentFragment;

    public static Intent getCallingIntent(Activity activity, LocationMapEntrance entrance) {
        Intent launch = new Intent(activity, LocationMapActivity.class);
        launch.putExtra(UIConstants.LocationMap.INTENT_KEY_STATION_FROM_DIRECTION, entrance);
        return launch;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleIntent();
        initializeFragment();
    }

    private void handleIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            mEntrance = (LocationMapEntrance) intent.getSerializableExtra(UIConstants.LocationMap.INTENT_KEY_STATION_FROM_DIRECTION);
        }
    }

    /**
     * 初始化显示内容
     */
    private void initializeFragment() {
        navigateToLocationMapFragment();
    }

    /**
     * 到关键词搜索页面
     *
     * @param
     */
    public void navigateToPOISearchFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        Fragment fragmentByTag = fragmentManager.findFragmentByTag(POISearchFragment.class.getSimpleName());
        if (fragmentByTag == null) {
            mPOISearchFragment = POISearchFragment.newInstance();
            transaction.add(R.id.frame_location_map_body, mPOISearchFragment, POISearchFragment.class.getSimpleName());
        } else {
            mPOISearchFragment.setInitialStatus();
        }

        transaction.hide(mLocationMapFragment);
        transaction.show(mPOISearchFragment);
        transaction.commitAllowingStateLoss();

        mCurrentFragment = mPOISearchFragment;
    }

    /**
     * 跳到地图页
     *
     * @param
     */
    public void navigateToLocationMapFragment() {
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        Fragment fragmentByTag = fragmentManager.findFragmentByTag(LocationMapFragment.class.getSimpleName());
        if (fragmentByTag == null) {
            mLocationMapFragment = LocationMapFragment.newInstance(mEntrance);
            transaction.add(R.id.frame_location_map_body, mLocationMapFragment, LocationMapFragment.class.getSimpleName());
        } else {
            mLocationMapFragment.setInitialStatus();
        }

        if (mPOISearchFragment != null) {
            transaction.hide(mPOISearchFragment);
        }
        transaction.show(mLocationMapFragment);
        transaction.commitAllowingStateLoss();

        mCurrentFragment = mLocationMapFragment;
    }

    @Override
    public void onBackPressed() {
        //关闭输入法键盘
        SoftInputUtils.closeInputKeyboard(this);

        if (mCurrentFragment instanceof POISearchFragment) {
            navigateToLocationMapFragment();
        } else {
            super.onBackPressed();
            overridePendingTransition(R.anim.noanimation, R.anim.right_out);
        }
    }


    public String getUserSelectCity() {
        return mUserSelectCity;
    }

    public void setUserSelectCity(String mSelectCity) {
        this.mUserSelectCity = mSelectCity;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_location_map;
    }

    @Override
    protected void initializeInjector() {
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {
    }


}
